package cz.tpj.logic;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cz.tpj.database.zamestnanec.Zamestnanec;

/**
 * Třídá, která se stará o načtení položek navigace v levém sloupci jsp stránek.
 * @author Martin
 */
public class Navigation {
	public final static String END_LINE = "\n";
	public final static String CONTEXT = "/TPJ/";
	public final static String DIV_CENTER = "center";
	//
	//public final static String ABSOLUT_SERVER_TEMP = "D:/EclipseDev/.metadata/.plugins/org.eclipse.wst.server.core/tmp3/wtpwebapps/TPJ/temp/";
	public final static String ABSOLUT_SERVER_TEMP = "C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/temp/TPJ/";
	public final static String ABSOLUT_SERVER_TEMP_CITIES_FOLDER = ABSOLUT_SERVER_TEMP + "cities/";
	public final static String JS_JQVERY = "javascripts/jquery.js";
	//public static final String TEMP_FOLDER = CONTEXT + "temp/";
	//public final static String TEMP_FOLDER = "http://localhost/temp/TPJ/";
	public final static String TEMP_FOLDER = "/temp/TPJ/";
	public final static String TEMP_CITYES = TEMP_FOLDER + "cities/";
	public final static String MEDIA_FOLDER = "C:/Program Files/Apache Software Foundation/media/";
	public final static String MEDIA_FOLDER_TEMP 	= MEDIA_FOLDER + "temp/";
	public final static String MEDIA_FOLDER_CITIES 	= MEDIA_FOLDER + "cities/";
    //konstantz pro session
    public static final String IS_ADMIN = "is_amd";
    public static final String EMPLOYEE = "employee";
    public static final String SESIION_MESSAGE = "session_message";
    //----slozky--------------
    public static final String FOLDER_SITES 				 = CONTEXT + "sites/";
    public static final String FOLDER_ATTRACTIVE_PLACE 		 = FOLDER_SITES + "attractive_place/";
    public static final String FOLDER_ATTRACTIVE_PLACE_ADMIN = FOLDER_ATTRACTIVE_PLACE + "admin_a_p/";
    public static final String FOLDER_ACTIVITIES 			 = FOLDER_SITES +"activities/";
    public static final String FOLDER_ADMIN 			 	 = FOLDER_SITES +"admin/";
    //konstanty obsahujici link na stranky (pripadne popisek)------
    public static final String JSP_HOME = "index.jsp";
    //---konstanty levy sloupec + ostatni JSP
    public static final String JSP_CULTURAL_INTERESTS   = FOLDER_SITES + "city.jsp";
    public static final String JSP_SEARCH_ROAD          = "sites/search_roat.jsp";
    public static final String JSP_INSERT_CITY          = FOLDER_ADMIN + "insert_city.jsp";
    public static final String JSP_INSERT_CITY_PICTURES = FOLDER_ADMIN + "insert_city_images.jsp";
    public static final String JSP_MODIFY_CITY          = "sites/admin/modify_city.jsp";
    public static final String JSP_ATTRACTIVE_PLACE		= FOLDER_ATTRACTIVE_PLACE + "attractive.jsp";
    public static final String JSP_ADD_ATTRACTIVE_PLACE	= FOLDER_ATTRACTIVE_PLACE_ADMIN + "add_atractive_pl.jsp";
    public static final String JSP_DEL_ATTRACTIVE_PLACE	= FOLDER_ATTRACTIVE_PLACE_ADMIN + "del_attractive_pl.jsp";
    public static final String JSP_MOD_ATTRACTIVE_PLACE	= FOLDER_ATTRACTIVE_PLACE_ADMIN + "mod_attractive_pl.jsp";
    public static final String JSP_ACTIVITIES 			= FOLDER_ACTIVITIES + "show_activities.jsp";
    public static final String JSP_HELP					= FOLDER_SITES + "help.jsp";
    //---vseobecne------------
    //zalogovani admina
    public static final String LOG_SITE           = "/sites/util/log_me.jsp";
    public static final String LOG_SITE_LABEL     = "Vstup do administrace";
    //odlogovani admina
    public static final String LOG_OUT            = "/sites/util/log_out.jsp";
    public static final String LOG_OUT_LABEL      = "odhlásit se z administrace";
    //----navigacni konstany
    public static final String LINK               = "link";
    //---servlety---
    public static final String SERVLET_UPLOAD = "upload";
	
    
    /**
     * Do ArryListu načte postupně všecky položky navigace v levém sloupci výsledné stránky.
     * @return ArrayList typu: {@link cz.tpj.logic.MenuItem}
     */
    public static ArrayList<MenuItem> getNavigationItems(){
        ArrayList<MenuItem> items = new ArrayList<MenuItem>();
        //nacteni jednotlivych polozek
        MenuItem itm = new MenuItem("Kulturní zajímavosti", JSP_CULTURAL_INTERESTS);
        items.add(itm);
        //itm = new MenuItem("Vyhledávání tras", TRAVEL_PLAN);
        items.add(new MenuItem("Vyhledávání tras", JSP_SEARCH_ROAD));
        items.add(new MenuItem("Přidat město"    , JSP_INSERT_CITY));
        items.add(new MenuItem("Upravit město"   , JSP_MODIFY_CITY));
        return items;
    }
    
    public static String getHead(String someMethaData){
    	String text = new String();
    	//---------------
    	text += "<head>" + END_LINE;
    	text += "	<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" + END_LINE;
    	text += "	<link rel='stylesheet' type='text/css' href='" + CONTEXT + "css/styl.css'>" + END_LINE;
    	text += "	<script charset='utf-8' id='injection_graph_func' src='" + CONTEXT + "css/injection_graph_func.js'></script>" + END_LINE;
    	
    	// pripad vlozeni metadat do hlavicky
    	if(someMethaData != null){
    		text += "	"+ someMethaData + END_LINE;
    	}
    	text += "	<title>TPJeseniky</title>" + END_LINE;
    	text += "</head>" + END_LINE;
    	//---------------
    	return text;
    }
    
    public static String getStartBody(){
    	String text = new String();
    	//-------
    	text += "<body>" + END_LINE;
    	text += "  <div id='main'>" + END_LINE;
    	text += "   <div id='head'>" + END_LINE;
    	text += "	  <a href='" + CONTEXT + JSP_HOME + "' title='Domovská stránka' style='display: block; width: 900px; height: 200px;'></a>" + END_LINE;
    	text += "	</div>" + END_LINE;
    	text += "	<div id='content_wrap'>" + END_LINE;
    	//-------
    	return text;
    }
    
    public static String getNavigationPanel(String actualSite, HttpServletRequest request){
    	//kontrolni inicializace aktualne vybrane stranky
    	if(actualSite == null){
    		actualSite = JSP_HOME;
    	}
    	HttpSession session = request.getSession(true);
    	boolean isAdmin = false;
    	if(session.getAttribute(IS_ADMIN) != null){
    		isAdmin = (Boolean) session.getAttribute(IS_ADMIN);
    	}
    	//postupne vkladani textu
        String text = new String();
        //--------
        text += "  <div id='left'>" + END_LINE;
        text += "	<div id='left_menu'>" + END_LINE;
        text += "	  <a href='"+Navigation.JSP_CULTURAL_INTERESTS+"'>Města</a>" + END_LINE;
        //sekce pro admina
        if(isAdmin){
        	text += "	  <a href='"+Navigation.JSP_INSERT_CITY+"'>Přidej město</a>" + END_LINE;
            text += "	  <a href='"+ Navigation.CONTEXT + Navigation.JSP_MODIFY_CITY+"'>Uprav město</a>" + END_LINE;
        }
        //
        text += "	  <a href='"+ Navigation.CONTEXT + Navigation.JSP_SEARCH_ROAD+"'>Najdi trasu</a>" + END_LINE;
        //
        text += "	  <a href='"+ Navigation.JSP_ATTRACTIVE_PLACE + "'>Bod zájmu</a>" + END_LINE;
        //sekce pro admina -- ZAJIMAVA MISTA --
        if(isAdmin){
        	text += "	  <a href='"+ Navigation.JSP_ADD_ATTRACTIVE_PLACE + "'>Přidej bod zájmu</a>" + END_LINE;
        	//text += "	  <a href='"+ Navigation.JSP_MOD_ATTRACTIVE_PLACE + "'>Uprav bod zájmu</a>" + END_LINE;
        	text += "	  <a href='"+ Navigation.JSP_DEL_ATTRACTIVE_PLACE + "'>Odstraň bod zájmu</a>" + END_LINE;
        }
        //
        text += "	  <a href='"+ Navigation.JSP_ACTIVITIES + "'>Aktivity</a>" + END_LINE;
        text += "	  <a href='"+ Navigation.JSP_HELP + "'>Nápověda</a>" + END_LINE;
        //
        text += "	</div>" + END_LINE;
        //
    	text += "	</div>" + END_LINE;
    	
        //--------
        return text;
    }

    public static String getLoginLink(HttpServletRequest request){
        String text = "";
        //---------
        text += "	<div id='right'>" + END_LINE;
        text += "	  <p style='text-align: center;'>" + END_LINE;
        //ziska session
        HttpSession session = request.getSession(true);
        //1) uzivatel neni prihlasen a vpravo ukaze odkaz sem
        //2) uzivatel je prihlasen a vpravo odkaz na odhlasovaci stranku
        if(session.getAttribute(Navigation.IS_ADMIN) == null || session.getAttribute(EMPLOYEE) == null){
        	text += "	  <a href='" + CONTEXT + LOG_SITE + "'>"+LOG_SITE_LABEL+"</a>" + END_LINE;
        }else{
        	Zamestnanec z = (Zamestnanec) session.getAttribute(EMPLOYEE);
        	text += "	  <h2>Jste přihlášen jako: " + z.getLogin() + "</h2>";
            text += "	  <a href='" + CONTEXT + LOG_OUT  + "'>"+LOG_OUT_LABEL+"</a>";
        }
        text += "	  </p>" + END_LINE;
        text += "	</div>" + END_LINE;
        text += "  </div>" + END_LINE;
        text += "  <div id='footer'>&copy; Turistický průvodce Jeseníky, 2009</div>" + END_LINE;
        text += "  </div>" + END_LINE;
        text += "</body>" + END_LINE;
        text += "<script type='text/javascript'><!--" + END_LINE;
        text += "function __RP_Callback_Helper(str, strCallbackEvent, splitSize, func){var event = null;if (strCallbackEvent){event = document.createEvent('Events');event.initEvent(strCallbackEvent, true, true);}if (str && str.length > 0){var splitList = str.split('|');var strCompare = str;if (splitList.length == splitSize)strCompare = splitList[splitSize-1];var pluginList = document.plugins;for (var count = 0; count < pluginList.length; count++){var sSrc = '';if (pluginList[count] && pluginList[count].src)sSrc = pluginList[count].src;if (strCompare.length >= sSrc.length){if (strCompare.indexOf(sSrc) != -1){func(str, count, pluginList, splitList);break;}}}}if (strCallbackEvent)document.body.dispatchEvent(event);}function __RP_Coord_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Coord_Callback = str;pluginList[index].__RP_Coord_Callback_Left = splitList[0];pluginList[index].__RP_Coord_Callback_Top = splitList[1];pluginList[index].__RP_Coord_Callback_Right = splitList[2];pluginList[index].__RP_Coord_Callback_Bottom = splitList[3];};__RP_Callback_Helper(str, 'rp-js-coord-callback', 5, func);}function __RP_Url_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Url_Callback = str;pluginList[index].__RP_Url_Callback_Vid = splitList[0];pluginList[index].__RP_Url_Callback_Parent = splitList[1];};__RP_Callback_Helper(str, 'rp-js-url-callback', 3, func);}function __RP_TotalBytes_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_TotalBytes_Callback = str;pluginList[index].__RP_TotalBytes_Callback_Bytes = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}function __RP_Connection_Callback(str){var func = function(str, index, pluginList, splitList){pluginList[index].__RP_Connection_Callback = str;pluginList[index].__RP_Connection_Callback_Url = splitList[0];};__RP_Callback_Helper(str, null, 2, func);}" + END_LINE;
        text += "//--></script>" + END_LINE;
        text += "" + END_LINE;
        return text;
    }
}
